<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Seo_m extends Base_model {
	public $primary_key = 'seo_id';
	public $description_word_limit = 156;

	function __construct() {
        parent::__construct();
    }
	
	function description_build($description = '')
	{
		return mb_substr($description, 0, $this->description_word_limit);
	}
	
	function seolink($string="",$limit=50,$id=0,$url_suffix=false)
	{
		if (!$string)
            return false;
		$string= $this->RemoveNonVietnamese($string);

		if ($string=="" && $id==0) return "";
		if ($string=="" && $id!=0) return $id;
		
		
		$badChars = array("á|a","à|a","ả|a","ã|a","ạ|a","â|a","ấ|a","ầ|a","ẩ|a","ẫ|a","ậ|a","ă|a","ắ|a","ằ|a","ẳ|a","ẵ|a","ặ|a","đ|d","é|e","è|e","ẻ|e","ẽ|e","ẹ|e","ê|e","ế|e","ề|e","ể|e","ễ|e","ệ|e","í|i","ì|i","ỉ|i","ĩ|i","ị|i","ó|o","ò|o","ỏ|o","õ|o","ọ|o","ô|o","ố|o","ồ|o","ổ|o","ỗ|o","ộ|o","ơ|o","ớ|o","ờ|o","ở|o","ỡ|o","ợ|o","ú|u","ù|u","ủ|u","ũ|u","ụ|u","ư|u","ứ|u","ừ|u","ử|u","ữ|u","ự|u","ý|y","ỳ|y","ỷ|y","ỹ|y","ỵ|y","Á|A","À|A","Ả|A","Ã|A","Ạ|A","Â|A","Ấ|A","Ầ|A","Ẩ|A","Ẫ|A","Ậ|A","Ă|A","Ắ|A","Ằ|A","Ẳ|A","Ẵ|A","Ặ|A","Đ|D","É|E","È|E","Ẻ|E","Ẽ|E","Ẹ|E","Ê|E","Ế|E","Ề|E","Ể|E","Ễ|E","Ệ|E","Í|I","Ì|I","Ỉ|I","Ĩ|I","Ị|I","Ó|O","Ò|O","Ỏ|O","Õ|O","Ọ|O","Ô|O","Ố|O","Ồ|O","Ổ|O","Ỗ|O","Ộ|O","Ơ|O","Ớ|O","Ờ|O","Ở|O","Ỡ|O","Ợ|O","Ú|U","Ù|U","Ủ|U","Ũ|U","Ụ|U","Ư|U","Ứ|U","Ừ|U","Ử|U","Ữ|U","Ự|U","Ý|Y","Ỳ|Y","Ỷ|Y","Ỹ|Y","Ỵ|Y");
		$size=count($badChars);
		for($i=0;$i<$size;$i++)	
		{		
			$test = explode("|",$badChars[$i]);
			$in = $test[0];
			$out = $test[1];
			$string=str_replace($in,$out,$string);
		}
		$string=url_title($string);	
		$string= trim(strtolower($string));
		if ($id!=0)
		{
			if ($url_suffix==true)
			$string=$string."-".$id.$this->CI->config->item("url_suffix");
			else
			$string=$string."-".$id;
		}
		else
		{
			if ($url_suffix==true)
			$string=$string.$this->CI->config->item("url_suffix");;	
			
		}	
		return $string;
	}
	
	function RemoveNonVietnamese($string){
		$string = preg_replace('/[^a-z0-9A-Z_ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềếểỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳýỵỷỹ \-()]/u', '', $string);	
		return $string;
	}
	 
	
	function DecimalToUni($str) {

	$convmap = array(
		0xe1,0xe1,0x0,0xe1,0xc1,0xc1,0x0,0xc1,0xe0,0xe0,0x0,0xe0,0xc0,0xc0,0x0,0xc0,0x1ea3,0x1ea3,0x0,0x1ea3
		,0x1ea2,0x1ea2,0x0,0x1ea2,0xe3,0xe3,0x0,0xe3,0xc3,0xc3,0x0,0xc3,0x1ea1,0x1ea1,0x0,0x1ea1,0x1ea0,0x1ea0,0x0,0x1ea0,
		0x103,0x103,0x0,0x103,0x102,0x102,0x0,0x102,0x1eaf,0x1eaf,0x0,0x1eaf,0x1eae,0x1eae,0x0,0x1eae,0x1eb1
		,0x1eb1,0x0,0x1eb1,0x1eb0,0x1eb0,0x0,0x1eb0,0x1eb3,0x1eb3,0x0,0x1eb3,0x1e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